package yana.tpe.shop.domain.dao.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import yana.tpe.shop.domain.dao.AbstractDao;
import yana.tpe.shop.domain.domains.AccountBook;

public class AccountBookDao implements AbstractDao<Void, AccountBook> {

    private File file;

    public AccountBookDao(File file) {
        this.file = file;
    }

    @Override
    public Void save(AccountBook value) {
        try {
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream ous = new ObjectOutputStream(fos);
            ous.writeObject(value);
            fos.close();
            return null;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public AccountBook get(Void value) {
        try {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Object readedObject = ois.readObject();
            fis.close();
            return (AccountBook) readedObject;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public void delete(Void key) {
        throw new UnsupportedOperationException("Not supported.");
    }

    @Override
    public List<AccountBook> list() {
        throw new UnsupportedOperationException("Not supported.");
    }

    public File getFile() {
        return file;
    }
}
